<?php
/**
 * Created by PhpStorm.
 * User: xieky
 * Date: 2018/1/11
 * Time: 11:46
 */

namespace mybackend\controllers;

use Yii;
use common\services\myhtsq\OutStorageService;


class OutStorageController extends BaseController
{
    protected $_outStorageService;

    public function __construct($id, $module, OutStorageService $outStorageService, $config = [])
    {
        $this->_outStorageService = $outStorageService;
        parent::__construct($id, $module, $config);
    }

    /**
     * 获取出库列表
     */
    public function actionGetDeliveryList(){
        $curpage = $this->getParam("pageindex",'post');
        $pagesize = $this->getParam("pagesize",'post');
        $keyword = $this->getParam("keyword",'post');
        $datatype = $this->getParam('datatype','post');
        $datastatus = $this->getParam('status','post');
        if(empty($curpage)){
            $curpage = 1;
        };
        if(empty($pagesize)){
            $pagesize = 20;
        };
        if(empty($keyword)){
            $keyword = '';
        };
        $result = $this->_outStorageService->GetDeliveryList($curpage,$pagesize,$keyword,$datatype,$datastatus);
        if($result['success'] === true){
            $this->exportJson($result['data'], '0', '');
        }else{
            $this->exportJson([], '-1', $result['errmsg']);
        }
    }

    /**
     * 获取出库明细
     */
    public function actionGetDeliveryDetail(){
        $deliveryGuid = $this->getParam("deliveryGuid",'post');
        if(empty($deliveryGuid)){
            $this->errorParam();
        }
        $result = $this->_outStorageService->GetDeliveryDetail($deliveryGuid);
        if($result['success'] === true){
            $this->exportJson($result['data'], '0', '');
        }else{
            $this->exportJson([], '-1', $result['errmsg']);
        }
    }

    /**
     * 更新出库状态
     */
    public function actionUpdateDeliveryState(){
        $deliveryGuid = $this->getParam("deliveryGuid",'post');
        $status = $this->getParam("status",'post');
        $reason = $this->getParam("reason",'post');
        $action = '业财打回';

        if(empty($deliveryGuid) || empty($status)){
            $this->errorParam();
        }
        $result = $this->_outStorageService->UpdateDeliveryState($deliveryGuid,$status);
        if($result['success'] === true){
            $result3 = $this->_outStorageService->AddOpreationRecord($deliveryGuid,$action,$reason);
            $this->exportJson($result['data'], '0', '');
        }else{
            $this->exportJson([], '-1', $result['errmsg']);
        }
    }

    /**
     * 添加操作记录
     */
    public function actionAddOpreationRecord(){
        $deliveryGuid = $this->getParam("deliveryGuid",'post');
        $action = '';
        $remarks = '';
        if(empty($deliveryGuid)){
            $this->errorParam();
        }
        $result = $this->_outStorageService->AddOpreationRecord($deliveryGuid,$action,$remarks);
        if($result['success'] === true){
            $this->exportJson($result['data'], '0', '');
        }else{
            $this->exportJson([], '-1', $result['errmsg']);
        }
    }

    /**
     * 读取工作流流程信息
     */
    public function actionGetWorkflowProcessList(){
        $deliveryGuid = $this->getParam("deliveryGuid",'post');
        if(empty($deliveryGuid)){
            $this->errorParam();
        }
        $result = $this->_outStorageService->GetWorkflowProcessList($deliveryGuid);
        if($result['success'] === true){
            $this->exportJson($result['data'], '0', '');
        }else{
            $this->exportJson([], '-1', $result['errmsg']);
        }
    }

    /**
     * 创建租户
     */
    public function actionAddTenant(){
        $deliveryGuid = $this->getParam("deliveryGuid",'post');
        if(empty($deliveryGuid)){
            $this->errorParam();
        }
        $result = $this->_outStorageService->AddTenant($deliveryGuid);
        if($result['success'] === true){
            $result3 = $this->_outStorageService->AddOpreationRecord($deliveryGuid,'开通租户','操作成功!');
            $result2 = $this->_outStorageService->UpdateDeliveryState($deliveryGuid,'22');
            if($result2['success'] === true) {
                $this->exportJson($result['data'], '0', $result['errmsg']);
            }else{
                $this->exportJson([], '-1', $result['errmsg'].' ,更新出库系统状态失败!');
            }
        }else{
            $result3 = $this->_outStorageService->AddOpreationRecord($deliveryGuid,'开通租户','操作失败!');
            $this->exportJson([], '-1', $result['errmsg']);
        }
    }

    /**
     * 租户授权
     */
    public function actionAuthTenant(){
        $deliveryGuid = $this->getParam("deliveryGuid",'post');
        if(empty($deliveryGuid)){
            $this->errorParam();
        }
        $result = $this->_outStorageService->AuthTenant($deliveryGuid);
        if($result['success'] === true){
            $result3 = $this->_outStorageService->AddOpreationRecord($deliveryGuid,'授权','授权成功!');
            $result2 = $this->_outStorageService->UpdateDeliveryState($deliveryGuid,'23');
            if($result2['success'] === true) {
                $this->exportJson($result['data'], '0', $result['errmsg']);
            }else{
                $this->exportJson([], '-1', '授权成功,更新出库系统状态失败!');
            }
        }else{
            $result3 = $this->_outStorageService->AddOpreationRecord($deliveryGuid,'授权','授权失败!');
            $this->exportJson([], '-1', $result['errmsg']);
        }
    }


    /**
     * 获取业务类型编号
     */
    public function actionWorkflowUrl()
    {
        $deliveryGuid = $this->getParam("deliveryGuid",'post');
        if(empty($deliveryGuid)){
            $this->errorParam();
        }
        $result = \Yii::$app->params['workflow_site'];

        if(empty($result)){
            $this->exportJson([], -1, '未配置工作流地址!');
        }else{
            $result = $result."/Pages/sq/tpcpcksq.aspx?type=yl&businessId=".$deliveryGuid;
            $this->exportJson($result, 0, 'success');
        }
    }

    /**
     * 读取报表列表
     */
    public function actionGetReportList(){
        $result = $this->_outStorageService->getReportList();
        if(empty($result)){
            $this->exportJson([], -1, '报表列表取数失败!');
        }else{
            $this->exportJson($result, 0, 'success');
        }
    }

    public function actionGetReportDetail(){
        $reportid = $this->getParam("reportid",'post');
        if(empty($reportid)){
            $this->errorParam();
        }
        $result = $this->_outStorageService->getReportDetail($reportid);
        if(empty($result)){
            $this->exportJson([], -1, '报表取数失败!');
        }else{
            $this->exportJson($result, 0, 'success');
        }
    }
}